package com.example.market_management.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.market_management.mapper.shopCarMapper;
import com.example.market_management.model.shopCar;
import com.example.market_management.model.vo.CommonResult;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Queue;

@RestController
@RequestMapping("shopCar")
@CrossOrigin
public class shopCarController {
    @Autowired
    private shopCarMapper shopCarMapper;

    /**
     * 添加购物车
     * @param userId
     * @param productId
     * @return
     */
    @PostMapping("/addShopCar")
    private CommonResult  addShopCar(Integer  userId,String productId){
        shopCar shopCar=new shopCar();
        shopCar.setAddTime(new Date());
        shopCar.setUserId(userId);
        shopCar.setProductId(productId);
        int i= shopCarMapper.insert(shopCar);
        if(i==1) return CommonResult.success("添加到购物车成功");
        else return CommonResult.success("添加到购物车失败");
    }

    /**
     * 删除购物车  多个购物车Id以,分隔
     * @param carId
     * @return
     */
    @DeleteMapping("/delShopCar")
    private CommonResult delShopCar(String carId){
        String[] carIds=carId.split(",");
        QueryWrapper  queryWrapper=new QueryWrapper();
        queryWrapper.in("car_id",carIds);
        int i= shopCarMapper.delete(queryWrapper);
        return  CommonResult.success("删除了"+i+"个购物车信息:",carIds);
    }

    /**
     * 查询购物车列表
     * @param userId
     * @return
     */
    @GetMapping("/shopCarList/{userId}")
    private  CommonResult shopCarList(@PathVariable  Integer userId){
        List<shopCar> shopCars=shopCarMapper.shopCarList(userId);
        return CommonResult.success("查询购物车列表成功",shopCars);
    }


    /**
     * 查询当前产品是否存在购物车内
     * @param userId
     * @param productId
     * @return
     */
    @GetMapping("/isExist/{userId}/{productId}")
    private  CommonResult isExist(int userId,String productId){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("product_id",productId);
        shopCar shopCar=shopCarMapper.selectOne(queryWrapper);
        if (shopCar!=null) return CommonResult.success("已存在购物车内",1);
        return CommonResult.failed("不存在购物车内",0);
    }

}
